package cn.hpclub.server.controller.h5.goventerprise;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.model.BusinessActionLog;
import cn.hpclub.server.model.MemberOrg;
import cn.hpclub.server.model.Notice;
import cn.hpclub.server.util.HttpUtil;
import cn.hpclub.server.util.SystemConfigUtil;

public class GovGroupH5Controller extends Controller{
    private static final Logger logger           = LoggerFactory.getLogger(GovGroupH5Controller.class);
    private static String       dprHostUrlPrefix = SystemConfigUtil.getSystemConfig().getCmccHostPrefix();
    private String              ESB_HOST_PREFIX  = SystemConfigUtil.getSystemConfig().getEsbHostPrefix();

    /**
     * 获取集团信息列表
     */
    public void list(){
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        String userId = getPara("userId"); // 实际就是userid
        int orgId = 0;
        if(StrKit.notBlank(userId)){
            orgId = MemberOrg.dao.getOrgIdById(Integer.valueOf(userId));
        }
        // logger.debug("storeMasterId:" + storeMasterId + ",orgId:" +
        // orgId);
        String keyword = getPara("keyword");
        Page<Record> pager = Notice.dao.getNoticeList(pageNumber, pageSize, keyword, orgId);
        renderJson(pager);
    }

    public void fakeQuery(){
        // String list =
        // "{'return':{'custMember':{'@nil':'true'},'custMemberList':{'@nil':'true'},'ff':{'@nil':'true'},'groupInfo':{'@nil':'true'},'groupInfoList':[{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800001166','grpName':'×××公安局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800001167','grpName':'成都市双流区人民政府办公室','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800002416','grpName':'北京飞机维修工程有限公司成都分公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800002643','grpName':'四川省双流县地方税务局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800002700','grpName':'国网四川省电力公司双流县供电分公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800003052','grpName':'成都市双流区市场监督管理局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋艳','serviceNo':'afdP04','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013489','grpName':'×××财政局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013492','grpName':'××××××司双流县分公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静芸','serviceNo':'afdP16','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013497','grpName':'双流县国土资源局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋艳','serviceNo':'afdP04','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013498','grpName':'××××家税务局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013505','grpName':'双流县司法局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013507','grpName':'中国人寿保险股份有限公司双流县支公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'吴薇','serviceNo':'afdP00005','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013508','grpName':'××××播电视局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋艳','serviceNo':'afdP04','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013513','grpName':'××××民检察院','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013521','grpName':'四川贵通建设集团有限公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'刘丽','serviceNo':'afdP92','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013528','grpName':'中国农业银行股份有限公司成都双流支行','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013532','grpName':'×××××险双流营业部','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'阚倩','serviceNo':'afdP15','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013538','grpName':'双流县教育局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013542','grpName':'四川弘民房地产开发责任有','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'刘丽','serviceNo':'afdP92','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013543','grpName':'×××卫生局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013545','grpName':'×××水务局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013547','grpName':'中国银行成都市双流县双流支行','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'陈惠','serviceNo':'afdP13','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013550','grpName':'中国工商银行双流县支行','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013553','grpName':'成都毅力汽车销售有限公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'阚倩','serviceNo':'afdP15','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800013557','grpName':'双流县兴能天然气有限公司','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'蒲珂妮','serviceNo':'afdP1u','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800020517','grpName':'××××展和改革局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800020519','grpName':'双流县科技局','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800020520','grpName':'中国共产党双流县委员会办公室','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800020521','grpName':'双流县共青团委员会','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}},{'consume':{'@nil':'true'},'doubleTerminalState':{'@nil':'true'},'entType':{'@nil':'true'},'entVNetScale':{'@nil':'true'},'grpId':'2800020522','grpName':'中共双流县委统战部','income':{'@nil':'true'},'isBelongMgr':{'@nil':'true'},'isSameOrg':{'@nil':'true'},'memberCount':{'@nil':'true'},'netOutRate':{'@nil':'true'},'serviceName':'宋静','serviceNo':'afdP44','userScope':{'@nil':'true'},'valueARPU':{'@nil':'true'}}],'nf':{'@nil':'true'},'notifyInfoList':{'@nil':'true'},'remindList':{'@nil':'true'},'reqNum':'201607061444233199','resInfoList':{'@nil':'true'},'resultCode':'00','resultDesc':null,'rspNum':'2016440602001951760','sb':{'@nil':'true'},'sf':{'@nil':'true'},'sr':{'@nil':'true'},'staffFunctionList':{'@nil':'true'},'staffList':{'@nil':'true'},'winfo':{'@nil':'true'},'wn':{'@nil':'true'},'workInfoDetail':{'@nil':'true'},'workNoteInfos':{'@nil':'true'},'workOrderInfos':{'@nil':'true'},'wv':{'@nil':'true'}},'rtnCode':1000,'rtnMsg':'ok'}";
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 30);
        String keyword = getPara("keyword");

        JSONObject obj = new JSONObject();
        obj.put("rtnCode", 1000);
        obj.put("rtnMsg", "xxx");
        JSONArray array = new JSONArray();

        JSONObject returnObj = new JSONObject();
        if(pageNumber == 15){
            JSONObject o = new JSONObject();
            o.put("empty", "true");
            returnObj.put("groupInfoList", o);
        } else{
            for(int i = 0; i < pageSize; i++){
                JSONObject o = new JSONObject();
                o.put("grpId", "id-" + keyword + "-" + pageNumber + "-" + (i + 1));
                o.put("grpName", "grpName-" + keyword + "-" + pageNumber + "-" + (i + 1));
                array.put(o);
            }
            returnObj.put("groupInfoList", array);
        }

        obj.put("return", returnObj);

        logger.debug("obj:" + obj);

        renderText(obj.toString());
        // renderJson(obj);
    }

    public void ws() throws IOException{
        String[] array = getRequest().getRequestURL().toString().split("/");
        String uri = array[array.length - 1];// getPara("uri");
        logger.debug("requestURL:" + uri);
        logger.debug("uri:" + uri);

        String data = getRequestBody();
        logger.debug("data:" + data);
        
        String esbHost = ESB_HOST_PREFIX.replaceAll("esop/rest/", "");
        String resultJsonString = getHttpByPostBody(esbHost + "esop/ws/" + uri, data);
        // resultJsonString = new
        // String(resultJsonString.getBytes("GBK"),"UTF-8");
        logger.debug("resultJsonString:" + resultJsonString);
        renderText(resultJsonString);
    }

    public void rest() throws IOException{
        String[] array = getRequest().getRequestURL().toString().split("/");
        String uri = array[array.length - 1];// getPara("uri");
        logger.debug("requestURL:" + uri);
        logger.debug("uri:" + uri);

        String token = getPara("token");
        logger.debug("token:" + token);

        String data = getRequestBody();
        logger.debug("data:" + data);

        String url = ESB_HOST_PREFIX + uri + "?token=" + token;
        logger.debug(" url:" + url);

        String resultJsonString = HttpUtil.getHttpByPostBody(url, data);
        logger.debug("resultJsonString:" + resultJsonString);
        renderText(resultJsonString);

    }

    /**
     *
     */
    public void crmRequest(){
        String uri = "";
        String resultJsonString = "";

        try{

            String[] array = getRequest().getRequestURL().toString().split("/");
            uri = array[array.length - 1];// getPara("uri");
            logger.debug("requestURL:" + uri);
            logger.debug("uri:" + uri);

            String token = getPara("token");
            logger.debug("token:" + token);

            String data = getRequestBody();
            logger.debug("data:" + data);
            String url = ESB_HOST_PREFIX + uri + "?token=" + token;
            logger.debug(" url:" + url);

            logger.debug("data:" + data);
            resultJsonString = getHttpByPostBody(url, data);
            logger.debug("resultJsonString:" + resultJsonString);
            renderJson(resultJsonString);
        }
        catch(Exception e){
            e.printStackTrace();
            String errorJsonString = "{\"ROOT\":{\"RETURN_CODE\":500,\"RETURN_MSG\":\"系统异常\"}}";
            renderJson(errorJsonString);
        }
        finally{
            saveLog(uri, resultJsonString);
        }
    }

    public void dprRequest(){
        String uriParam = "";
        String resultJsonString = "";

        try{
            uriParam = getUriParamValue();
            logger.debug("urlParam:" + uriParam);
            String url = dprHostUrlPrefix + uriParam;
            logger.debug("url:" + url);
            String data = getRequestBody();
            logger.debug("data:" + data);
            resultJsonString = getHttpByPostBody(url, data);
            if(resultJsonString.startsWith("Error")){
                resultJsonString = "{\"resCode\":\"0000001\",\"resMsg\":\"" + resultJsonString
                        + "\",\"detailMsg\":\"\",\"outData\":\"\"}";
            }

            logger.debug("resultJsonString:" + resultJsonString);
            renderJson(resultJsonString);
        }
        catch(Exception e){
            e.printStackTrace();
            resultJsonString = "{\"resCode\":\"0000002\",\"resMsg\":\"" + e.getMessage()
                    + "\",\"detailMsg\":\"\",\"outData\":\"\"}";
            renderJson(resultJsonString);
        }
        finally{
            saveLog(uriParam, resultJsonString);
        }
    }

    /**
     * @Title: saveLog
     * @Description:TODO
     * @param uriParam
     * @param resultJsonString
     *            void
     */

    private void saveLog(String uriParam, String resultJsonString){
        int type = getBusinessType(uriParam);
        logger.debug("type=" + type);
        if(type == -1){
            return;
        }
        try{
            Map<String, String> params = getDprRequesParams(uriParam);
            String orderId = "";
            int status = 0;
            if(!resultJsonString.startsWith("Error")){
                JSONObject jo = new JSONObject(resultJsonString.toLowerCase());
                if("0000000".equals(jo.getString("rescode"))){
                    status = 1;
                    orderId = getOrderId(type, jo);
                    logger.debug("orderId=" + orderId);
                }
            }
            saveBusinessTransactionLog(uriParam, resultJsonString, type, params, orderId, status);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Title: saveBusinessTransactionLog
     * @Description:TODO
     * @param uriParam
     * @param resultJsonString
     * @param type
     * @param params
     * @param orderId
     * @param status
     *            void
     */

    private void saveBusinessTransactionLog(String uriParam, String resultJsonString, int type,
            Map<String, String> params, String orderId, int status){
        BusinessActionLog log = getModel(BusinessActionLog.class);
        log.set("business_type", type);
        log.set("phone", params.get("mobile_no"));
        String businessCode = getBusinessCode(params);
        log.set("business_code", businessCode);
        log.set("user_id", params.get("userId"));
        log.set("store_id", params.get("storeId"));
        log.set("login_no", params.get("login_no"));
        log.set("user_id", params.get("userId"));
        log.set("url", uriParam);
        log.set("status", status);
        log.set("server_response", resultJsonString);
        log.set("order_id", orderId);
        log.set("time", new Date());
        log.save();
    }

    /**
     * @Title: getOrderId
     * @Description:TODO
     * @param type
     * @param jo
     * @return String
     */

    private String getOrderId(int type, JSONObject jo){
        String key = getBusinessCodeNameByType(type);
        String orderId = "";
        try{
            JSONObject outData = (JSONObject)jo.get("outdata");
            orderId = outData.getString(key);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return orderId;
    }

    /**
     * @Title: getBusinessCodeNameByType
     * @Description:TODO
     * @param type
     * @return String
     */

    private String getBusinessCodeNameByType(int type){
        String key = "";
        /* 办理成功，获取OrderID */
        switch(type){
            case 0:
                key = "login_accept";
                break;
            default:
                key = "order_id";
                break;
        }
        return key;
    }

    /**
     * @Title: getBusinessCode
     * @Description:TODO
     * @param params
     *            void
     */

    private String getBusinessCode(Map<String, String> params){
        String business_code = params.get("prod_prcid");
        if(business_code == null){
            business_code = params.get("means_id");
        }
        if(business_code == null){
            business_code = params.get("back_code");
        }
        if(business_code == null){
            business_code = "";
        }
        return business_code;
    }

    /**
     * @Title: getBusinessType
     * @Description:TODO
     * @param uriParam
     * @return int
     */

    private int getBusinessType(String uriParam){
        int type = -1;
        if(uriParam.startsWith("/system/sShortAddMode.json")){
            type = 1; // 附加资费
        } else if(uriParam.startsWith("/system/updateMainCharges.json")){
            type = 0;// 主资费变更
        } else if(uriParam.startsWith("/system/sTreatyDeal.json")){
            if(uriParam.contains("means_id")){
                type = 2;// 营销活动
            } else{
                type = 3;// 营销活动(短信)
            }
        }
        return type;
    }

    /**
     * @Title: getDprRequesParams
     * @Description:TODO
     * @param uriParam
     *            void
     */

    private Map<String, String> getDprRequesParams(String uriParam){
        Map<String, String> maps = new HashMap<String, String>();
        String requestParams = uriParam.split("\\?")[1];
        logger.debug("requestParams:" + requestParams);
        String[] params = requestParams.split("&");
        for(String param : params){
            String[] array = param.split("=");
            String key = array[0];
            String value = array[1];
            logger.debug(key + ":" + value);
            maps.put(key, value);
        }
        return maps;
    }

    public void bdRequest(){
        String[] array = getRequest().getRequestURL().toString().split("/");
        String uri = array[array.length - 1];// getPara("uri");
        logger.debug("requestURL:" + uri);
        logger.debug("uri:" + uri);
        String params = getParams();
        logger.debug("parms:" + params);

        String url = SystemConfigUtil.getSystemConfig().getCmccHostPrefix() + "/broadband/" + uri + ".json?" + params;
        logger.debug(" url:" + url);
        String resultJsonString = getHttpByPostBody(url, null);
        logger.debug("resultJsonString:" + resultJsonString);

        renderJson(resultJsonString);
    }

    private String getParams(){
        String params = "";
        Enumeration<String> paraNames = getParaNames();
        while(paraNames.hasMoreElements()){
            String paramName = (String)paraNames.nextElement();
            String[] paramValues = getParaValues(paramName);
            if(paramValues.length == 1){
                String paramValue = paramValues[0];
                if(params.length() == 0){
                    params += paramName + "=" + paramValue;
                } else{
                    params += "&" + paramName + "=" + paramValue;
                }
            }
        }
        return params;
    }

    private String getUriParamValue(){
        String params = "";
        Enumeration<String> paraNames = getParaNames();
        while(paraNames.hasMoreElements()){
            String paramName = (String)paraNames.nextElement();
            if(paramName.equals("uri")){
                String[] paramValues = getParaValues(paramName);
                if(paramValues.length == 1){
                    params = paramValues[0];
                    break;
                }
            }
        }
        try{
            logger.debug("decode uri:" + URLDecoder.decode(params, "UTF-8"));
            return URLDecoder.decode(params, "UTF-8").replace("&amp;", "&");
        }
        catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @return
     * @throws IOException
     *             String
     * @Title: getRequestBody
     * @Description:TODO
     */
    private String getRequestBody() throws IOException{
        InputStream is = getRequest().getInputStream();
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while((ch = is.read()) != -1){
            bytestream.write(ch);
        }
        is.close();
        byte[] result = bytestream.toByteArray();

        String data = new String(result);
        return data;
    }

    public static String getHttpByPostBody(String url, String data){
        HttpURLConnection connect = null;
        String error = "";
        try{

            connect = (HttpURLConnection)new java.net.URL(url).openConnection();
            connect.setRequestMethod("POST");
            connect.setDoOutput(true);
            connect.setDoInput(true);
            connect.setUseCaches(false);
            connect.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
            connect.setConnectTimeout(60000);
            connect.setReadTimeout(60000);

            DataOutputStream out = new DataOutputStream(connect.getOutputStream());
            if(data != null){
                out.writeBytes(data);
                out.flush();
                out.close();
            }
            connect.connect();
            int responseCode = connect.getResponseCode();
            logger.debug("status=" + responseCode);
            if(responseCode == 200){
                InputStream is = connect.getInputStream();
                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                int ch;
                while((ch = is.read()) != -1){
                    bytestream.write(ch);
                }
                is.close();
                byte[] result = bytestream.toByteArray();
                return new String(result);
            } else{
                return "Error:remote server error. status code is " + responseCode;
            }
        }
        catch(Exception e){
            e.printStackTrace();
            error = e.getMessage();
        }
        finally{
            if(null != connect){
                connect.disconnect();
            }
        }
        return "Error:" + error;
    }

}
